package com.wuyanzu.shixicoderedo.controller;


import com.wuyanzu.shixicoderedo.common.dto.LoginDTO;
import com.wuyanzu.shixicoderedo.common.dto.RegisterDTO;
import com.wuyanzu.shixicoderedo.common.entity.User;
import com.wuyanzu.shixicoderedo.common.utils.ResponseVO;
import com.wuyanzu.shixicoderedo.common.vo.RegisterVO;
import com.wuyanzu.shixicoderedo.service.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

// 注册 / 登录
@RestController
@RequestMapping("/api/auth")
public class AuthController {
    @Autowired
    private AuthService authService;

    /**
     * 注册
     * @param registerDTO
     * @return
     */
    @PostMapping("/register")
    public ResponseVO register(@RequestBody RegisterDTO registerDTO) {

        RegisterVO register = authService
                .register(registerDTO.getUsername(),
                        registerDTO.getPassword(),
                        registerDTO.getDisplayName());

        return ResponseVO.success
                ("200", register.getUsername());
    }

    @PostMapping("/login")
    public ResponseVO login(@RequestBody LoginDTO loginDTO) {
        String token = authService.login(loginDTO.getUsername(), loginDTO.getPassword());
        return ResponseVO.success("200", token);
    }


}
